package com.example.customvidewbyheroes;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

public class TopBar extends RelativeLayout {

    private Button mLeftButton,mRightButton;

    private topBarClickListener mListener;

    public TopBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(R.styleable.TopBar);
        int mLeftTextColor = array.getColor(R.styleable.TopBar_leftTextColor, 0);
        Drawable mLeftBackground = array.getDrawable(R.styleable.TopBar_leftBackground);
        String mLeftText = array.getString(R.styleable.TopBar_leftText);
        int mRightTextColor = array.getColor(R.styleable.TopBar_rightText, 0);
        Drawable mRightBackground = array.getDrawable(R.styleable.TopBar_rightBackground);
        String mRightText = array.getString(R.styleable.TopBar_rightText);
        float mTitleTextSize = array.getDimension(R.styleable.TopBar_titleTextSize, 10);
        int mTitleTextColor = array.getColor(R.styleable.TopBar_titleTextColor, 0);
        String mTitle = array.getString(R.styleable.TopBar_title);
        array.recycle();

        mLeftButton = new Button(context);
        mRightButton = new Button(context);
        TextView mTitleView = new TextView(context);

        // 为创建的组件元素赋值
        // 值就来源于我们在引用的xml文件中给对应属性的赋值
        mLeftButton.setTextColor(mLeftTextColor);
        mLeftButton.setBackground(mLeftBackground);
        mLeftButton.setText(mLeftText);

        mRightButton.setTextColor(mRightTextColor);
        mRightButton.setBackground(mRightBackground);
        mRightButton.setText(mRightText);

        mTitleView.setText(mTitle);
        mTitleView.setTextColor(mTitleTextColor);
        mTitleView.setTextSize(mTitleTextSize);
        mTitleView.setGravity(Gravity.CENTER);

        LayoutParams mLeftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);

        addView(mLeftButton, mLeftParams);

        LayoutParams mRightParams = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);

        mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(mRightButton, mRightParams);

        LayoutParams mTitleParams = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(mTitleView, mTitleParams);

        mRightButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                    mListener.rightClick();
            }
        });

        mLeftButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                    mListener.leftClick();
            }
        });

    }




    public void setOnTopBarClickListener(topBarClickListener listener){
        this.mListener = listener;
    }


    public interface topBarClickListener{
        void leftClick();
        void rightClick();
    }


    public void setButtonVisable(int id,boolean flag){
        if(flag){
            if(id == 0){
                mLeftButton.setVisibility(View.VISIBLE);
            }else {
                mRightButton.setVisibility(View.VISIBLE);
            }
        }else {
            if(id == 0){
                mLeftButton.setVisibility(View.GONE);
            }else {
                mRightButton.setVisibility(View.GONE);
            }
        }
    }
}
